#ifndef LUA_STATE_BUILDING
#define LUA_STATE_BUILDING
#include <luaState/config.h>
#include <vector>
#include <string>
#include <list>
#include <complex>
#include <mutex>
#include <boost/filesystem.hpp>
#include <boost/function.hpp>

struct lua_State;
namespace lua{

	class
	LUA_STATE_EXPORT_API
	luaState{
	public:
		luaState();
		luaState(lua_State*);
		luaState(const luaState&)=delete;
		void setState(lua_State*);
		lua_State* state();
		virtual ~luaState();
		virtual void openLibs();
		virtual void registerGlobalFunction();
		int openLib(boost::function< int(lua_State*) > fun);
		std::shared_ptr<std::mutex> mutex() { return m_mutex; }
		void setPath(const char* path);
		void initEnv();
		void addTempPath( const char* str );    //添加目录
		void addPath( const char* str );        //添加目录
		void addCPath( const char* str );       //添加目录
		void initPath();                        //初始化目录
		void initCPath();                       //初始化目录
		void addPath(const std::string& str){addPath(str.c_str());};
		void addCPath(const std::string& str){addCPath(str.c_str());};
		virtual int setTempDir(const char* path){return 0;};
		virtual int stopFlag();
		virtual int stopSolverFlag();
		virtual int suspendFlag();
		virtual void setStopFlag(int set);
		virtual void setStopSolverFlag(int set);
		virtual void setSuspendFlag(int set);
	public:
		virtual void initState(){};                //初始化Lua环境
		virtual void preSave(){};                //打开文件前的初始化Lua环境
		virtual void postLoad(){};
		virtual void initProject(){};            //打开文件前的初始化Lua环境

	public:
		int doFile(const char* fName);
		int doFile(const std::string& str){ return doFile(str.c_str());};
		int doString(const char* str);
		int doString(const std::string& str){return doString(str.c_str());};
		int callFun(const char* str);
		int callFun(const std::string& str){return callFun(str.c_str());};
		virtual int doFileImpl(const char* fName);
		virtual int doStringImpl(const char* fName);
		virtual int callFunImpl(const char* fName);

	public:
		virtual int loadFile(const char* fName);
		virtual int doCall(int narg, int clear);
		virtual int loadString(const char* fName);
		int loadString(const std::string& str){return loadString(str.c_str());};

	public:
		void errorHandle();
		int errorState(){return _err;};
		std::string errorString() const{ return _errStr; }
	public:
	protected:
		lua_State* _state;
		int _autoClose;
		int _err;
		std::string _errStr;
		boost::filesystem::path _filePath;
		std::vector< std::string > _paths,_cpaths;
		std::shared_ptr<std::mutex> m_mutex,m_flagMutex;
		int _stopFlag; //停止脚本
		int _stopSolverFlag; //停止求解器
		int _suspendFlag;
	};
};

#endif
